<?php
class UrlManager extends CUrlManager
{
	private $_erules=array();
	private static $exc = array('/\/page-[0-9]*/'); 
	private $excGet = array('page-'); 
	private $page = 'page'; 
	
	
    public function createUrl($route,$params=array(),$ampersand='&')
    {
        if (!isset($params['language'])) {
            if (Yii::app()->user->hasState('language'))
                Yii::app()->language = Yii::app()->user->getState('language');
            else if(isset(Yii::app()->request->cookies['language']))
                Yii::app()->language = Yii::app()->request->cookies['language']->value;
            $params['language']=Yii::app()->language;
        }
        $controller = Yii::app()->controller->getId();
       	$url = parent::createUrl($route, $params, $ampersand);
       	if(isset($params['url_id']) && $route=='e/url'){
	       	$urlModel = Urls::model()->findByPk($params['url_id']);
	       	$url = isset($urlModel->alias) ? '/'.Yii::app()->language.'/'.$urlModel->alias : str_replace($controller.'/', '', $url);
       	} else {
       		$url = str_replace($controller.'/', '', $url);
       	}
        return $url;
    }
    public function parseUrl($request)
    {
    	if($this->getUrlFormat()===self::PATH_FORMAT)
    	{
    		$rawPathInfo=$request->getPathInfo();
    		$pathInfo=$this->removeUrlSuffix($rawPathInfo,$this->urlSuffix);
    		$pathInfoMain=$this->removeLnSuffix($pathInfo);
    		$pathInfo=$this->removeExSuffix($pathInfoMain);
    		$page = Urls::model()->findByAlias($pathInfo);
    		if($page){
    			$this->createGet($pathInfoMain, $page->get_param);
    			$pathInfo = $page->route;
    		} else {
    			$pageInfo = $pathInfo;
    		}
    		
    		foreach($this->_erules as $i=>$rule)
    		{
    			if(is_array($rule))
    				$this->_rules[$i]=$rule=Yii::createComponent($rule);
    			if(($r=$rule->parseUrl($this,$request,$pathInfo,$rawPathInfo))!==false)
    				return isset($_GET[$this->routeVar]) ? $_GET[$this->routeVar] : $r;
    		}
    		if($this->useStrictParsing)
    			throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
    					array('{route}'=>$pathInfo)));
    		else
    			return $pathInfo;
    	}
    	else if(isset($_GET[$this->routeVar]))
    		return $_GET[$this->routeVar];
    	else if(isset($_POST[$this->routeVar]))
    		return $_POST[$this->routeVar];
    	else
    		return '';
    }
    public function removeLnSuffix($pathInfo)
    {
    	foreach (Yii::app()->params['languages'] as $key => $value){
	    	$urlSuffix = $key;
	    	if($urlSuffix!=='' && substr($pathInfo, 0, strlen($urlSuffix))===$urlSuffix){
	    		$pathInfo = substr($pathInfo, strlen($urlSuffix)+1);
	    	}
    	}
    	return $pathInfo;
    }
    public function removeExSuffix($pathInfo)
    {
    	foreach (self::$exc as $key => $value){
    		$pathInfo = preg_replace($value, '', $pathInfo);
    	}
    	return $pathInfo;
    }
    public function createGet($pathInfoMain, $param)
    {
    	$post = explode(';', $param);
    	$path = explode('/', $pathInfoMain);
    	foreach($path as $key => $value){
    		if(isset($post[$key])){
	    		if($post[$key]=='page'){
		    		if(strpos($value, $this->page)!==false) {
		    			$_GET[$post[$key]] = str_replace($this->excGet, '', $value);
		    		} else {
		    			$_GET[$post[0]] = str_replace($this->excGet, '', $value);
		    		}
	    		} else {
	    			$_GET[$post[$key]] = str_replace($this->excGet, '', $value);
	    		}
    		} else {
    			$_GET[$post[0]] = str_replace($this->excGet, '', $value);
    		}
    	}
    	//BaseController::debug($_GET);exit;
    }
}
?>